package com.bootdo.blog.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.annotation.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.blog.domain.DrugDO;
import com.bootdo.blog.service.DrugService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;


/**
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-03-06 21:38:13
 */
@Controller
@RequestMapping("/blog/drug")
public class DrugController {
    @Autowired
    private DrugService drugService;

    @GetMapping()
    @RequiresPermissions("blog:drug:drug")
    String Drug() {
        return "blog/drug/drug";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("blog:drug:list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<DrugDO> drugList = drugService.list(query);
        int total = drugService.count(query);
        PageUtils pageUtils = new PageUtils(drugList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("blog:drug:save")
    String add() {
        return "blog/drug/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("blog:drug:update")
    String edit(@PathVariable("id") Integer drugId, Model model) {
        DrugDO drugDO = drugService.get(drugId);
        model.addAttribute("drugDO", drugDO);
        return "blog/drug/edit";
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{drugId}")
    @RequiresPermissions("blog:drug:info")
    public R info(@PathVariable("drugId") Integer drugId) {
        DrugDO drug = drugService.get(drugId);
        return R.ok().put("drug", drug);
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @Log("保存施药信息")
    @RequiresPermissions("blog:drug:save")
    public R save(DrugDO drug) {
        if (drugService.save(drug) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("修改施药信息")
    @RequiresPermissions("blog:drug:update")
    public R update(DrugDO drug) {
		System.out.println(drug);
        drugService.update(drug);

        return R.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @Log("删除施药信息")
    @ResponseBody
    @RequiresPermissions("blog:drug:remove")
    public R remove(@RequestParam("id") Integer drugId) {
        if (drugService.remove(drugId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @Log("删除施药信息")
    @ResponseBody
    @RequiresPermissions("blog:drug:remove")
    public R remove(@RequestParam("ids[]") Integer[] drugIds) {
        drugService.batchRemove(drugIds);

        return R.ok();
    }

}
